/**
 * 1. 实现功能
 * 制作面包
 */

function main() {
  console.log('开业'),
    console.log('烤制黑面包')
  console.log('烤制蜂蜜面包')
  console.log('烤制奶油面包')
}

/**
 * 2. 新增面包师
 * 
 * 面包之间高度耦合
 */

class BreadMaker {
  static getBeard(breadType: number) {
    switch (breadType) {
      case 1:
        console.log('烤制黑面包')
        break;
      case 2:
        console.log('烤制蜂蜜面包')
        break;
      case 3:
        console.log('烤制奶油面包')
        break;
    }
  }
}

function main2() {
  console.log('开业'),
    console.log('顾客需要黑面包')
  BreadMaker.getBeard(1)
  console.log('顾客需要蜂蜜面包')
  BreadMaker.getBeard(2)
  console.log('顾客需要奶油面包')
  BreadMaker.getBeard(3)
}

/**
 * 3. 静态工厂
 * 
 * 违背 开放-封闭原则
 */
class BreadMaker_3 {
  getBread() { }
}

class BlackBread extends BreadMaker_3 {
  getBread(): void {
    console.log('烤制黑面包')
  }
}
class HoneyBread extends BreadMaker_3 {
  getBread(): void {
    console.log('烤制蜂蜜面包')
  }
}
class WhiteBread extends BreadMaker_3 {
  getBread(): void {
    console.log('烤制牛奶面包')
  }
}

class BreadFactory {
  static makeBreak(breadType: number) {
    let breadMaker: BreadMaker_3 | null = null
    switch (breadType) {
      case 1:
        breadMaker = new BlackBread()
        break;
      case 2:
        breadMaker = new HoneyBread()
        break;
      case 3:
        breadMaker = new WhiteBread()
        break;
      default:
        break;
    }
    return breadMaker
  }
}

function main3() {
  let breadMaker: BreadMaker_3 | null
  console.log('顾客需要黑面包')
  breadMaker = BreadFactory.makeBreak(1)
  breadMaker?.getBread()
  console.log('顾客需要蜂蜜面包')
  breadMaker = BreadFactory.makeBreak(2)
  breadMaker?.getBread()
  console.log('顾客需要奶油面包')
  breadMaker = BreadFactory.makeBreak(3)
  breadMaker?.getBread()
}

/**
 * 工厂模式
 */
interface IFactory {
  CreateBread(): BreadMaker_3
}

class BlackBreadFactory implements IFactory {
  CreateBread() {
    return new BlackBread()
  }
}
class HoneyBreadFactory implements IFactory {
  CreateBread() {
    return new HoneyBread()
  }
}
class WhiteBreadFactory implements IFactory {
  CreateBread() {
    return new WhiteBread()
  }
}

(function () {
  let breadMaker: BreadMaker_3 | null

  console.log('顾客需要黑面包')
  let breadFactory: IFactory = new BlackBreadFactory()
  breadMaker = breadFactory.CreateBread()
  breadMaker.getBread()
  console.log('顾客需要蜂蜜面包')
  breadFactory = new HoneyBreadFactory()
  breadMaker = breadFactory.CreateBread()
  breadMaker.getBread()
  console.log('顾客需要奶油面包')
  breadFactory = new WhiteBreadFactory()
  breadMaker = breadFactory.CreateBread()
  breadMaker.getBread()
}())